<?php

namespace app\admin\controller;

use app\common\model\CepingWeidu as CepingWeiduModel;
use think\Db;
 
class CepingWeidu extends Base
{

    /**
     * [index 列表]
     * @return [type] [description]
     * @author felix
     */
	public function index(){
		
		$data['cp_id'] = input('cp_id');
		$data['Nowpage'] = input('get.page') ? input('get.page'):1;
		
		$map['status'] = 1;
		if($data['cp_id'] && $data['cp_id']!==""){
			$map['cp_id'] = $data['cp_id'];
		}
		$list = CepingWeiduModel::where($map)->order('id asc')->paginate(config('list_rows'))->toArray();
		$data['count'] = $list['total'];
		$data['allpage'] = $list['last_page'];  
		$return_date = $list['data'];
		if(input('get.page')){
			return json($return_date);
		}
		$this->assign($data);
		return $this->fetch();
    }


    public function ScoreEdit()
    {
        $goods = new CepingWeiduModel();
        $page = input('page');
        if(request()->isAjax()){

			$param = input('post.');
			$param['score_options'] = json_encode(array_values($param['score_options']),JSON_UNESCAPED_UNICODE);
			$res = CepingWeiduModel::update($param, ['id' => $param['id']]);
			if ($res) {
				$this->success('操作成功');
			} else {
				$this->error('操作失败');
			}
        }

        $id = input('param.id');
		$info = CepingWeiduModel::where('id',$id)->find();
        $this->assign('info',$info);
        $this->assign('page', $page);		
		$cp_id = input('cp_id');
        $this->assign('cp_id', $cp_id);
		$ceping_title = Db::name('ceping')->where('id',$cp_id)->value('name');
        $this->assign('ceping_title', $ceping_title);
		
		if(!empty($info['score_options'])){
			$options_list = json_decode($info['score_options'],true);
		}else{
			$score_options = '[{"title":"高分组","score":"3.5","info":""},{"title":"中分组","score":"2.5","info":""},{"title":"低分组","score":"0","info":""}]';
			$options_list = json_decode($score_options,true);
		}
		
		//dump($info);die;
		$options_count = !empty($options_list)?count($options_list):0;
        $this->assign('options_list',$options_list);
        $this->assign('options_count',$options_count);
        return $this->fetch();
    }
	

    /**
     * @return [type] [description]
     * @author felix
     */
    public function add()
    {
        
        if(request()->isAjax()){

            $param = input('post.');
            $obj = new CepingWeiduModel();
			$res = $obj->allowField(true)->save($param);
			if ($res) {
				$this->success('添加成功');
			} else {
				$this->error('添加失败');
			}
        }
		$cp_id = input('cp_id');
        $this->assign('cp_id', $cp_id);
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function edit()
    {
        if(request()->isAjax()){

            $param = input('post.');
			$res = CepingWeiduModel::update($param, ['id' => $param['id']]);
			if ($res) {
				$this->success('编辑成功');
			} else {
				$this->error('编辑失败');
			}
        }
        $id = input('param.id');
		$info = CepingWeiduModel::where('id',$id)->find();
        $cp_id = input('cp_id');
        $this->assign('cp_id', $cp_id);
        $this->assign('info',$info);
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function del()
    {
        $id = input('param.id');
		$res = CepingWeiduModel::where('id',$id)->delete();
		if ($res) {
			$this->success('删除成功');
		}else {
			$this->error('删除失败');
		}
    }

}